﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DatabaseCompare.Model;
using DatabaseCompare.Service.Compare;

namespace DatabaseCompare.ServiceImpl.Compare
{
    public class FunctionListCompare : ICompareAbleListCompare<FunctionInfo, FunctionCompareResult>
    {
        /// <summary>
        /// 比较类.
        /// </summary>
        private ICompareAbleCompare<FunctionInfo, FunctionCompareResult> functionCompare = new FunctionCompare();


        public List<FunctionCompareResult> Compare(List<FunctionInfo> leftFunctionInfoList, List<FunctionInfo> rightFunctionInfoList)
        {
            // 结果.
            List<FunctionCompareResult> resultList = new List<FunctionCompareResult>();

            // 为避免下面的 Linq 处理异常
            // 假如 一个参数为 null
            // 将其设置为一个 空的 List.
            if (leftFunctionInfoList == null)
            {
                leftFunctionInfoList = new List<FunctionInfo>();
            }
            if (rightFunctionInfoList == null)
            {
                rightFunctionInfoList = new List<FunctionInfo>();
            }


            // 使用 Linq 
            // 检索两个的  的并集。
            // 同时合并掉 相同的.
            var query =
                (from function in leftFunctionInfoList
                 select function.Name
                    ).Union
                (from function in rightFunctionInfoList
                 select function.Name
                    );


            // 获取 名 List.
            List<string> functionNameList = query.ToList();

            // 名排序.
            functionNameList.Sort();


            // 依次比较.
            for (int i = 0; i < functionNameList.Count; i++)
            {
                // 检索左边
                var queryLeft =
                    from data in leftFunctionInfoList
                    where data.Name == functionNameList[i]
                    select data;

                // 检索右边
                var queryRight =
                    from data in rightFunctionInfoList
                    where data.Name == functionNameList[i]
                    select data;


                // 比较结果.
                FunctionCompareResult functionCompareResult = null;

                if (queryLeft.Count() == 0)
                {
                    if (queryRight.Count() == 0)
                    {
                        // 左找不到， 右也找不到
                        // 理论上这种情况不会发生.
                        functionCompareResult = functionCompare.Compare(null, null);
                    }
                    else
                    {
                        // 左找不到， 右找到
                        functionCompareResult = functionCompare.Compare(null, queryRight.First());
                    }
                }
                else
                {
                    if (queryRight.Count() == 0)
                    {
                        // 左找到， 右找不到
                        functionCompareResult = functionCompare.Compare(queryLeft.First(), null);
                    }
                    else
                    {
                        // 左找到， 右找到
                        functionCompareResult = functionCompare.Compare(queryLeft.First(), queryRight.First());
                    }
                }


                // 将 比较结果加入最后的比较List
                resultList.Add(functionCompareResult);

            }

            return resultList;
        }

    }
}
